import * as makeup from "./makeup.js"
import * as renderer from "./renderer-pixi.js"
import * as utils from "./utils.js"
import * as test from "./test.js"

//export { makeup, renderer }

function init(cfg) {
	console.log('app init after window loaded')
	cfg =cfg || {}
	var width = cfg.width || 464;
	var height = cfg.height || 320;
	var dom = cfg.dom || document.body;
	renderer.init(width,height,dom)

	makeup.init({
		width: 464,
		height: 320
	}, [{
		id: 1,
		x: 0,
		y: 0,
		width: 20,
		height: 20
	}, {
		id: 2,
		x: 20,
		y: 0,
		width: 20,
		height: 20
	}, {
		id:3,
		x: 0,
		y: 20,
		width: 20,
		height: 20
	}, {
		id: 4,
		x: 20,
		y: 20,
		width: 20,
		height: 20
	}, {
		id: 5,
		x: 80,
		y: 80,
		width: 20,
		height: 20
	}, {
		id: 6,
		x: 100,
		y: 80,
		width: 20,
		height: 20
	}, {
		x: 120,
		y: 80,
		width: 20,
		height: 20
	}, {
		id: 7,
		x: 80,
		y: 100,
		width: 20,
		height: 20
	}, {
		id: 8,
		x: 100,
		y: 100,
		width: 20,
		height: 20
	}, {
		x: 120,
		y: 100,
		width: 20,
		height: 20
	}, {
		id: 9,
		x: 80,
		y: 120,
		width: 20,
		height: 20
	}, {
		id: 10,
		x: 100,
		y: 120,
		width: 20,
		height: 20
	}, {
		id: 11,
		x: 120,
		y: 120,
		width: 20,
		height: 20
	}, {
		id: 13,
		x: 80,
		y: 0,
		width: 80,
		height: 80
	}]);
	makeup.each(makeup.placedCards(),function(rect){
		gom.push({ rect:rect,gobj:renderer.drawRect(rect) })
	})
}
window.addEventListener("load", function() {
	//init();
})

var placedCards = [];

var gom = [];
function sameRect(a,b){
	return a == b || (a.x == b.x && a.y==b.y && a.width == b.width && a.height ==b.height)
}

function findGom(rect){
	var gs = gom.filter((item)=>{
		return item.rect && (item.rect == rect || (rect.id && rect.id == item.rect.id) || sameRect(rect,item.rect)) ;
	})
	console.log(gs)
	return gs.length ? gs[0] : null;
}
function moveRect(rect){
	var gobj = findGom(rect);
	if(gobj && gobj.gobj){
		var x = rect.x || Math.random()*464;
		var y = rect.y || Math.random()*320;
		gobj.gobj.beginFill(0xAAAAAA);
		gobj.gobj.drawRect(0, 0, gobj.rect.width, gobj.rect.height);
		gobj.gobj.endFill();
		
		gobj.gobj.x = x;
		gobj.gobj.y = y;
	}
}
function removeRect(rect) {
	console.log("removeRect", rect)
	makeup.each(gom, function(item) {
		if(item.rect && (item.rect == rect || sameRect(rect,item.rect)) && item.gobj){
			renderer.app.stage.removeChild(item.gobj);
		}
	})
	//renderer.app.stage.removeChild(renderer.app.stage.children[3]); //.remove(3)
}
function test1(){
	var ps =makeup.findLivePoints();
	console.log(ps)
}

function resize(w,h){
	renderer.resize(w,h)
	
}
export {
	removeRect,moveRect,resize,test1
}
export default { init}
//export { makeup.findLivePoints as findLivePoints }